package control;

import database.Database;
import java.sql.SQLException;
import model.TombStone;

/**
 * Denne klasse håndterer manipulation af TombStone
 * @author Sonny Sandberg
 */
public abstract class TombStoneControl 
{
    /**
     * Tilføj TombStone til databasen
     * @param TombStone
     * @return int
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static int addTombStonePart(TombStone tombStone) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        
        int cuffin = utility.Converter.booleanToInt(tombStone.isCuffin());
        String area = tombStone.getArea();
        int row = tombStone.getTheRow();
        int number = tombStone.getTheNumber();
        int roomFor = tombStone.getNumNamesRoomFor();
        
        String oldName = "";
        for (int i = 0; i < tombStone.getOldName().size(); i++)
        {
            oldName += tombStone.getOldName().get(i);
            if (i < tombStone.getOldName().size()-1)
            {
                oldName += "\n";
            }            
        }
        
        String inscription = "";
        for (int i = 0; i < tombStone.getInscription().size(); i++)
        {
            inscription += tombStone.getInscription().get(i);
            if (i < tombStone.getInscription().size()-1)
            {
                inscription += "\n";
            }            
        }
        
        String font = tombStone.getFont();
        int repaint = utility.Converter.booleanToInt(tombStone.isRepaint());
        double commission = tombStone.getCommision();
        
        int mortician_commission_no = 0;
        
        // Selvom dette i databasen er en int, skal vi have en null værdi, i tilfælde af, der ikke er nogen bedemand tilknyttet
        // Forsøges det at sætte 0 (zero) i stedet, returnerer databasen parent/child constraint fejl
        // En int kan ikke være null i JAVA, der strengen
        String morticianID = null;
        if (tombStone.getMortician().getCustomerName() != null)
        {
            if (tombStone.getMortician().getCustomerName().getId() != 0)
            {
                mortician_commission_no = model.handlers.TombStoneHandler.getNextCommissionID(tombStone);
                morticianID = ""+tombStone.getMortician().getCustomerName().getId();
            }
        }
        
        // Selvom dette i databasen er en int, skal vi have en null værdi, i tilfælde af, der ikke er nogen kirkegård tilknhttet
        // Forsøges det at sætte 0 (zero) i stedet, returnerer databasen parent/child constraint fejl
        // En int kan ikke være null i JAVA, der strengen
        String graveYardID = null;
        if (tombStone.getGraveYard() != null)
        {
            if (tombStone.getGraveYard().getId() != 0)
            {
                graveYardID = ""+tombStone.getGraveYard().getId();
            }
        }
        
        int added = db.manipulateDB("INSERT INTO bs_tombstone_order "
                + "(cuffin, area, the_row, the_number, old_name, room_for, inscription, font, repaint, commission, mortician_commission_no, fk_mortician_id, fk_grave_yard_id) "
                + "VALUES"
                + " ('"+cuffin+"', '"+area+"', '"+row+"', '"+number+"', '"+oldName+"', '"+roomFor+"', '"+inscription+"', '"+font+"', '"+repaint+"', '"+commission+"', "
                + "'"+mortician_commission_no+"', "+morticianID+", "+graveYardID+")");
        db.closeDB();
        
        return added;
    }
}